package com.iamalive;

import com.iamalive.LocalCache.RecipientDetails;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Context;


public class RegisterNewRecipient extends Activity {

	Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_new_recipient);
		
		mContext = getApplicationContext();
		Button buttonOk = (Button) findViewById(R.id.buttonRegisterRecipientOk);
		Button buttonCancel = (Button) findViewById(R.id.buttonRegisterRecipientCancel);

		buttonOk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onOkButtonClicked();
            }
        });
		
		buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
	}
	
	private void onOkButtonClicked() {
		EditText editTextName;
		EditText editTextEmail;
		EditText editTextPhone;
		EditText editTextAddress;
		EditText editTextRelationship;
		
		String name, email, phone, address, relation;
		
		String toastMessage = new String();
		
		editTextName = (EditText) findViewById(R.id.editTextRegisterRecipientName);
		editTextEmail = (EditText) findViewById(R.id.editTextRegisterRecipientEmail);
		editTextPhone = (EditText) findViewById(R.id.editTextRegisterRecipientPhone);
		editTextAddress = (EditText) findViewById(R.id.editTextRegisterRecipientAddress);
		editTextRelationship = (EditText) findViewById(R.id.editTextRegisterRecipientRelationship);
		
		name = editTextName.getText().toString();
		email = editTextEmail.getText().toString();
		phone = editTextPhone.getText().toString();
		address = editTextAddress.getText().toString();
		relation = editTextRelationship.getText().toString();
		
		if((false == name.isEmpty())
				&& ((false == email.isEmpty())
					|| (false == phone.isEmpty())
					|| (false == address.isEmpty())
					|| (false == relation.isEmpty()))) {
			if(validateInfo(email, phone) == true) {
				LocalCache localCache = LocalCache.getSingleInstance();
				if(localCache.doesRecipientAlreadyExist(name)) {
					toastMessage = "The recipient " + name + " already exists...";
					Utils.showToast(mContext, toastMessage);
					return;
				}
				//TODO: register new recipient by calling webservice
				
				//Also store into localcache
				RecipientDetails newRecipient = localCache.new RecipientDetails();
				//newRecipient.id =	//TODO set id of this recipient received from web service call
				newRecipient.name = name;
				newRecipient.email = email;
				newRecipient.phone = phone;
				newRecipient.address = address;
				newRecipient.relationship = relation;
				localCache.addNewRecipient(newRecipient);

				//show a confirmation toastmessage 	
				toastMessage = "The recipient " + name + " has been registered";
				Utils.showToast(mContext, toastMessage);				
				finish();
			} else {
				return;
			}			
		} else {
			toastMessage = "Name and at least one of email id, phone or address is mandatory";
			Utils.showToast(mContext, toastMessage);
			return;
		}
	}
	
	private boolean validateInfo(String email, String phone) {
		String toastMessage = new String();
		if(email.isEmpty() == false) {
			if(Utils.isValidEmailAddress(email) == false) {
				toastMessage = "The email address " + email + " is illegal";
				Utils.showToast(mContext, toastMessage);
				return false;
			}
		}
		
		if(phone.isEmpty() == false) {
			if(Utils.isValidPhoneNum(phone) == false) {
				toastMessage = "The phone no. " + email + " is illegal";
				Utils.showToast(mContext, toastMessage);
				return false;
			}
		}
		
		return true;
	}

}
